
/*
'===============================================================================
'  HNG.Portal.BL.TblSupportOnlineManager
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace HNG.Portal.BLL
{
	public class TblSupportOnlineManager : TblSupportOnlineManagerBase
	{
        SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public TblSupportOnlineManager()
		{
			// Nothing for now.
		}

        public DataTable Get_Bykeysearch(string keysearch, string columnName, int langID)
        {
            string strQuery = "select * from TblSupportOnline ";
            if (columnName != "")
                strQuery += " where " + columnName + " like N'%" + keysearch + "%'" + " AND langID=" + langID + " Order by ID desc";
            else
                strQuery += " where HoTen like N'%" + keysearch + "%'" + " AND langID=" + langID + " Order by ID desc";
            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, strQuery);
        }

        public DataTable SelectCommon(TblSupportOnlineEntity _TblSupportOnlineEntity)
        {
            System.Text.StringBuilder sbSql = new System.Text.StringBuilder();
            try
            {
                sbSql.Append("WITH tblTemp AS").Append("\n");
                sbSql.Append("(").Append("\n");
                sbSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY ");
                sbSql.AppendFormat("ch.{0} ", string.IsNullOrEmpty(_TblSupportOnlineEntity.SortExpression) ? "[CreateDate]" : _TblSupportOnlineEntity.SortExpression);
                sbSql.Append(string.IsNullOrEmpty(_TblSupportOnlineEntity.SortDirection) ? " DESC" : _TblSupportOnlineEntity.SortDirection);
                sbSql.Append(") AS RowNum,").Append("\n");
                sbSql.Append("  ch.[ID],").Append("\n");
                sbSql.Append("  ch.[LangID],").Append("\n");
                sbSql.Append("  ch.[HoTen],").Append("\n");
                sbSql.Append("  ch.[DienThoai],").Append("\n");
                sbSql.Append("  ch.[Phone],").Append("\n");
                sbSql.Append("  ch.[Email],").Append("\n");
                sbSql.Append("  ch.[ChucVu],").Append("\n");
                sbSql.Append("  ch.[TypeSupport],").Append("\n");
                sbSql.Append("  ch.[NickSupport],").Append("\n");
                sbSql.Append("  ch.[TrangThai],").Append("\n");
                sbSql.Append("  lng.[Name] LangName").Append("\n");
                sbSql.Append("  FROM").Append("\n");
                sbSql.Append("  TblSupportOnline ch").Append("\n");
                sbSql.AppendFormat("  LEFT JOIN tblLang lng").Append("\n");
                sbSql.Append("  ON lng.ID = ch.LangID").Append("\n");
                sbSql.Append("  WHERE").Append("\n");
                sbSql.Append("  1 = 1").Append("\n");

                if (_TblSupportOnlineEntity.Id != 0 )
                {
                    sbSql.AppendFormat("  AND ch.[ID] LIKE {0}", _TblSupportOnlineEntity.Id).Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblSupportOnlineEntity.Condition))
                {
                    sbSql.AppendFormat("  AND (ch.[ID] LIKE N'%' + {0} + '%'", _TblSupportOnlineEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[LangID] LIKE N'%' + {0} + '%'", _TblSupportOnlineEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[HoTen] LIKE N'%' + {0} + '%'", _TblSupportOnlineEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[DienThoai] LIKE N'%' + {0} + '%'", _TblSupportOnlineEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Phone] LIKE N'%' + {0} + '%'", _TblSupportOnlineEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Email] LIKE N'%' + {0} + '%'", _TblSupportOnlineEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[ChucVu] LIKE N'%' + {0} + '%'", _TblSupportOnlineEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TypeSupport] LIKE N'%' + {0} + '%')", _TblSupportOnlineEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[NickSupport] LIKE N'%' + {0} + '%')", _TblSupportOnlineEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TrangThai] LIKE N'%' + {0} + '%')", _TblSupportOnlineEntity.Condition).Append("\n");

                }

                if (_TblSupportOnlineEntity.LangId != 0 || _TblSupportOnlineEntity.LangId != null)
                {
                    sbSql.AppendFormat("  AND ch.[LangId] ={0}", _TblSupportOnlineEntity.LangId).Append("\n");
                }

                if (_TblSupportOnlineEntity.TrangThai.ToString().Equals("True"))
                {
                    sbSql.AppendFormat("  AND ch.[TrangThai] ={0}", 1).Append("\n");
                }


                sbSql.Append(")").Append("\n");
                sbSql.Append("SELECT *, (SELECT Max(RowNum) FROM tblTemp) TotalItems FROM tblTemp").Append("\n");
                sbSql.AppendFormat("WHERE RowNum BETWEEN {0} AND {1}", _TblSupportOnlineEntity.StartIndex, _TblSupportOnlineEntity.EndIndex).Append("\n");
            }
            catch { }
            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, sbSql.ToString());
        }
	}
}
